<?php
/**
 * 分类服务层
 * @author maniac
 */
namespace app\common\service;
use app\common\logic\Dump;
use think\Db;

class Categories extends Base
{
    // 查询字段
    public static $field = 'id,name';

    /**
     * 获取区域
     * @param  integer $provinceId 省份 id
     * @param  integer $cityId     城市 id
     * @param  integer $districtId 区域 id
     * @return array
     */
    public function categories($category_id = 0, $category_id1 = 0, $category_id2 = 0)
    {
        if (empty($this->addr_data)) {
            $this->initData();
        }
        if (!$category_id) {
            foreach ($this->addr_data as $key => $val) {
                $data[] = [
                    'id'   => $val['id'],
                    'name' => $val['name'],
                ];
            }
            return $data;
        }
        $data = $this->addr_data[$category_id]['child'];
        if (!$category_id1) {
            foreach ($data as $key => $val) {
                $data[$key] = [
                    'id'   => $val['id'],
                    'name' => $val['name'],
                ];
            }
            return array_values($data);
        }
        $data = $data[$category_id1]['child'];
        if ($category_id2) {
            $data = $data[$category_id2];
        } else {
            $data = array_values($data);
        }
        return $data;
    }

    /**
     * 获取地区信息
     * @return array
     */
    public function getAddrData()
    {
        if (empty($this->addr_data)) {
            $this->initData();
        }
        return $this->addr_data;
    }

    /**
     * init
     */
    private function initData()
    {
        $arr  = Db::name('categories')->where('type','ads')->field('id,name')->select();
//        dump($arr);die;
        $arr1  = Db::name('ads_place')->where('parent_id','0')->field('id,name,type_id,parent_id')->select();
        $arr2  = Db::name('ads_place')->where('parent_id','>','0')->field('id,name,type_id,parent_id')->select();

        foreach ($arr1 as &$v1){
            $v1['child'] = [];
            foreach ($arr2 as &$v2){
                if($v2['parent_id'] == $v1['id']){
                    $v1['child'][] =$v2;
                }
            }
        }
        foreach ($arr as &$v){
            $v['child'] = [];
            foreach ($arr1 as &$v1){
                if($v1['type_id'] == $v['id']){
                    $v['child'][] =$v1;
                }
            }
        }
        $data = [];
        foreach ($arr as &$v) {
            $temp2 = [];
            foreach ($v['child'] as $vv) {
                $temp3 = [];
                foreach ($vv['child'] as $vvv) {
                    $temp3[$vvv['id']] = [
                        'id' => $vvv['id'],
                        'parent_id' => $vvv['parent_id'],
                        'name' => $vvv['name'],
                    ];
                }
                $temp2[$vv['id']] = [
                    'id' => $vv['id'],
                    'parent_id' => $vv['type_id'],
                    'name' => $vv['name'],
                    'child' => $temp3,
                ];
            }
            $data[$v['id']] = [
                'id' => $v['id'],
                'parent_id' => 0,
                'name' => $v['name'],
                'child' => $temp2,
            ];
        }
        $this->addr_data = $data;
    }
}
